package com.chis.jmdatatimer.main;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.sql.hive.HiveContext;
import org.springframework.core.io.ClassPathResource;

import java.util.Properties;

/**
 * 
 * @Description 
 * @Company zwx
 * @author wlj
 * @version 2018年12月11日
 */
public class HiveCon {

    public static void main(String[] args) {
        try {
            System.out.println("===========处理开始================");
            Properties properties = new Properties();
            properties.load(new ClassPathResource("config.properties").getInputStream());
            System.setProperty("SPARK_YARN_MODE", "true");

//		System.setProperty("HADOOP_USER_NAME", "root");
            SparkConf conf=new SparkConf();
            conf.setAppName("hive_convert_task");
            conf.setMaster(properties.getProperty("spark.master"));
            conf.set("spark.yarn.dist.files", properties.getProperty("spark.yarn.dist.files"));
            conf.set("spark.yarn.jars", properties.getProperty("spark.yarn.jars"));

//            conf.set("spark.executor.memory", "512m");
//            conf.set("spark.executor.core", "1");
//            conf.set("spark.yarn.queue", "root.real");
            conf.set("spark.serializer","org.apache.spark.serializer.KryoSerializer");
            JavaSparkContext ctx = new JavaSparkContext(conf);

            HiveContext hiveContext = new HiveContext(ctx);
            hiveContext.sql("use "+properties.getProperty("hivedb"));
            hiveContext.sql("select * from "+properties.getProperty("hivetable")).repartition(Integer.valueOf(properties.getProperty("hive.numbers"))).registerTempTable("inoculation_tmp");
            hiveContext.sql("insert overwrite table "+properties.getProperty("hivetable")+" select * from inoculation_tmp");
            hiveContext.dropTempTable("inoculation_tmp");
            System.out.println("===========处理结束================");
        } catch (Exception e){
            e.printStackTrace();
        }

    }

}
